import java.util.Arrays;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xiaotutu
 * Date: 2025-01-12
 * Time: 18:13
 */




public class Test {

    private static void merge(int[] array,int left,int mid,int right) {

        int s1 = left;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = right;

        int[] tmpArr = new int[right-left+1];
        int k = 0;//tmpArr数组的下标

        while (s1 <= e1 && s2 <= e2) {
            if(array[s1] <= array[s2]){
                tmpArr[k++] = array[s1++];
            }else {
                tmpArr[k++] = array[s2++];
                //s2++;
                //k++;
            }
        }
        while (s1 <= e1) {
            tmpArr[k++] = array[s1++];
        }
        while (s2 <= e2) {
            tmpArr[k++] = array[s2++];
        }
        for (int i = 0; i < k; i++) {
            array[i+left] = tmpArr[i];
        }

    }

    private static void mergeSortFunc(int[] array, int left, int right) {
        // 这里不应该取 >= 号 已经调试了 == 是可以的
        if(left == right) {
            return;
        }
        int mid = (left+right) / 2;
        mergeSortFunc(array,left,mid);
        mergeSortFunc(array,mid+1,right);
        merge(array,left,mid,right);//合并
    }

    /**
     * 归并排序递归实现
     * @param array
     */
    public static void mergeSort(int[] array) {
        mergeSortFunc(array,0,array.length-1);
    }


    /**
     * 非递归实现快速排序
     * @param array
     */
    public static void quickSort1(int[] array) {

        Stack<Integer> stack = new Stack<>();
        int start = 0;
        int end = array.length-1;

        if(end - start +1 <= 20) {
            //直接插入排序
            insertSort2(array,start,end);
            return;
        }

        //三数取中
        int mid = threeNum(array,start,end);
        //交换
        swap(array,mid,start);/**/

        int pivot = partition(array,start,end);
        if(pivot > start+1) {
            stack.push(start);
            stack.push(pivot-1);
        }

        if(pivot < end-1) {
            stack.push(pivot+1);
            stack.push(end);
        }

        while (!stack.empty()) {
            end = stack.pop();
            start = stack.pop();

            if(end - start +1 <= 20) {
                //直接插入排序
                insertSort2(array,start,end);
                //return; 这个地方不能return
            }else {
                mid = threeNum(array,start,end);
                //交换
                swap(array,mid,start);/**/

                pivot = partition(array, start, end);
                if (pivot > start + 1) {
                    stack.push(start);
                    stack.push(pivot - 1);
                }
                if (pivot < end - 1) {
                    stack.push(pivot + 1);
                    stack.push(end);
                }
            }
        }
    }

    /**
     * 冒泡排序
     * 时间复杂度：O(N^2) 对数据不敏感  有序 无序都是这个复杂度！
     * 空间负责度：O(1)
     * 稳定性：稳定的排序
     *     插入排序   冒泡排序
     *
     * 加了优化之后，时间复杂度可能会变成O(n)
     * @param array
     */
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length- 1- i; j++) {
                if (array[j] > array[j + 1]) {
                    swap(array, j, j + 1);
                    flg = true;
                }
            }
            if (!flg) {
                break;
            }
        }
    }

    /**
     * hoare法
     * @param array
     * @param left
     * @param right
     * @return
     */
    private static int partition1(int[] array, int left, int right) {
        int i = left; // 记录初始位置
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            while (left < right && array[left] <= tmp) {
                left++;
            }
            swap(array, left, right);
        }
        swap(array, left, i);
        return left;
    }

    /**
     * 挖坑法
     * @param array
     * @param left
     * @param right
     * @return
     */
    private static int partition(int[] array, int left, int right) {

        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }
    /**
     * 前后指针法
     * @param array
     * @param left
     * @param right
     * @return
     */
    private static int partition2(int[] array, int left, int right) {
        int prev = left ;
        int cur = left+1;
        while (cur <= right) {
            if(array[cur] < array[left] && array[++prev] != array[cur]) {
                swap(array,cur,prev);
            }
            cur++;
        }
        swap(array,prev,left);
        return prev;
    }

    /**
     * 三数取中法
     * @param array
     * @param left
     * @param right
     * @return 返回中间数字的下标
     */
    private static int threeNum(int[] array, int left, int right) {
        int mid = (left + right) / 2;
        if (array[left] < array[right]) {
            if (array[mid] < array[left]) {
                return left;
            }else if (array[mid] > array[right]  ) {
                return right;
            }else {
                return mid;
            }
        }else {
            if (array[mid] < array[right]) {
                return right;
            }else if (array[mid] > array[left]) {
                return left;
            }else {
                return mid;
            }
        }
    }

    public static void insertSort2(int[] array, int left, int right) {
        for (int i = left + 1; i <= right; i++) {
            int j = i - 1;
            int tmp = array[i];
            for (; j >= left; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                }else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    private static void quick(int[] array, int start, int end) {
        if (start >= end) {
            return;
        }
        // 递归到小的子区间时, 可以考虑使用直接插入排序
        if (end - start + 1 <= 20) {
            insertSort2(array, start, end);
            return;
        }

        int mid = threeNum(array, start, end);
        swap(array, start, mid);
        int pivot = partition(array, start, end);

        quick(array, start, pivot -1 ); //左树
        quick(array, pivot + 1, end);  // 右树
    }

    /**
     * 堆排序
     * @param array
     */
    public static void heapSort(int[] array) {
        createBigHeap(array);
        int end = array.length - 1;
        while (end > 0) {
            swap(array, 0, end);
            siftDown(array, 0, end);
            end--;
        }
    }

    public static void createBigHeap(int[] array) {
        for (int parent = (array.length-1-1) / 2; parent >= 0; parent--) {
            siftDown(array, parent, array.length);
        }
    }

    public static void siftDown(int[] array, int parent, int size) {

        int child = 2 * parent + 1;
        while (child < size) {
            if(child + 1 < size && array[child] < array[child + 1]) {
                child = child + 1;
            }
            if (array[parent] < array[child]) {
                swap(array, parent, child);
                parent = child;
                child = 2 * parent + 1;
            }else {
                break;
            }
        }
    }


    public static void selectSort2(int[] array) {
        int left = 0;
        int right = array.length-1;
        while (left < right) {
            int minIndex= left;
            int maxIndex = left;
            for (int i = left + 1; i <= right; i++) {
                if (array[i] < array[minIndex]) {
                    minIndex = i;
                }
                if (array[i] > array[maxIndex]) {
                    maxIndex = i;
                }
            }
            swap(array, left, minIndex);
            if (maxIndex == left) {
                maxIndex = minIndex;
            }
            swap(array, right, maxIndex);
            left++;
            right--;
        }
    }

    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array, i, minIndex);
        }
    }

    private static void swap(int[] array, int left, int right) {
        int tmp = array[left];
        array[left] = array[right];
        array[right] = tmp;
    }


    /**
     * 希尔排序
     * @param array
     * @param gap
     */
    public static void shellSort(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (array[j] > tmp) {
                    array[j + gap] = array[j];
                }else {
                    break;
                }
            }
            array[j + gap] = tmp;
        }
    }

    public static void shell(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            gap /= 2;
            shellSort(array, gap);
        }
    }

    /**
     * 插入排序
     * @param array
     */
    public static void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                }else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }


    public static void main(String[] args) {
        int[] array = new int[] {9, 8,7, 2,3,4,5,10,6};
        // insertSort(array);
        // shell(array);
        selectSort(array);
        // heapSort(array);
        System.out.println(Arrays.toString(array));
    }
}
